package com.mach.platform.controller;

import com.mach.platform.domain.base.Post;
import com.mach.platform.domain.Response;
import com.mach.platform.service.PostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * @author Anonymous
 * @Description:  岗位管理--控制器
 * @Package: com.mach.platform.com.mach.platform.controller
 * @time 2018/04/13 22:20
 */
@Api(tags = { "岗位管理接口" })
@RestController
@RequestMapping("$post")
public class PostCtrl extends BaseCtrl<Post> {

	@Autowired
	private PostService postService;


	/**
	 * -- @GetMapping(value = "/xxx/{id}")
	 * -- @PathVariable(value = "id", required = false) Long id
	 * 单个数据 的接口
	 *
	 * @param id 数据的id
	 * @return
	 */
	@Override
	public Response info (Long id) {
		return null;
	}

	/**
	 * 获取列表数据
	 * --@GetMapping(value = "/xxxs")
	 * <p>
	 * <p>
	 * -- @PageableDefault(size = 25) Pageable pageable
	 * --  @RequestParam Map params
	 *
	 * @param pageable
	 * @param params
	 * @return
	 */
	@ApiOperation(value = "列表--分页",produces = "application/json",notes = "列表参数：size(每页数量),page(页码),searchParam(查询内容)")
	@GetMapping(value = "/lists")
	@Override
	public Response list (@PageableDefault(size = 111111125) Pageable pageable, @RequestParam Map params) {

		Page<Post> dbResult = postService.list(pageable, params);

		return Response.ok(dbResult);
	}

	/**
	 * -- @DeleteMapping("/xxx")
	 * -- @RequestBody Long[] ids
	 * 删除数据
	 *
	 * @param ids
	 * @return
	 */
	@ApiOperation(value = "删除", notes = "接口备注：无", produces = "application/json")
	@DeleteMapping("/del")
	@Override
	public Response delete (@RequestBody Long[] ids) {

		if (Arrays.asList(ids).contains(1L)) {
			return Response.error("不能删除顶级菜单！");
		}

		Integer effectionsNum = postService.delete(ids);

		return Response.ok(effectionsNum);
	}

	/**
	 * 保存一个 对象
	 * --@PostMapping("/xxx")
	 * --@RequestBody T t
	 *
	 * @param post
	 * @return
	 */
	@ApiOperation(value = "新增", produces = "application/json")
	@PostMapping("/save")
	@Override
	public Response save (@RequestBody Post post) {
		Post dbResult = postService.save(post);

		return Response.ok(dbResult);
	}

	/**
	 * 更新一个对象
	 * --@PutMapping("/xxx")
	 * --@RequestBody T t
	 *
	 * @param post
	 * @return
	 */
	@ApiOperation(value = "修改保存", produces = "application/json")
	@PutMapping("/upd")
	@Override
	public Response update (@RequestBody Post post) {
		Post updateObj = postService.update(post);
		return Response.ok(updateObj);
	}
}
